package cn.edu.lzu.service.impl;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.dao.*;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.Permission;
import cn.edu.lzu.entity.ProfessionalTitle;
import cn.edu.lzu.entity.Role;
import cn.edu.lzu.service.RoleService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: cn.edu.lzu.service.impl
 * @ClassName: RoleServiceImpl
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/5/6 10:53
 * @Version: 1.0
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private ExaminerMapper examinerMapper;
    @Resource
    private NationMapper nationMapper;
    @Resource
    private ProfessionalTitleMapper professionalTitleMapper;

    @Override
    public DataPage getRoles(long unitId) {
        DataPage dataPage=new DataPage();
        List<Role> roleList=roleMapper.selRolesTrue(unitId);
        dataPage.setCode(0);
        dataPage.setTotalCount(roleList.size());
        dataPage.setData(roleList);
        return dataPage;
    }

    @Override
    public ResponseResult batchDeleteRolesById(String roleIds) {
        roleIds=roleIds.substring(0, roleIds.length()-1);
        int result=roleMapper.updDeleteRolePerms(roleIds);
        int roleResult=roleMapper.updDeleteRole(roleIds);
        System.out.println(roleResult+"\t"+result);
        ResponseResult responseResult = new ResponseResult();
        if (roleResult == roleIds.split(",").length) {
            responseResult.setCode(1);
            responseResult.setMsg("角色批量删除成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("角色批量删除失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult addRole(long unitId, String role, String roleName, String perms) {
        ResponseResult responseResult=new ResponseResult();
        Role role1=roleMapper.selByRoleAndName(role, roleName, unitId);
        if(role1!=null){
            responseResult.setCode(0);
            responseResult.setMsg("新建失败，该角色已经存在！");
            return responseResult;
        }
        int addRoleResult=roleMapper.insRole(role, roleName, unitId);
        if(addRoleResult!=1){
            responseResult.setCode(0);
            responseResult.setMsg("新建角色失败！");
            return responseResult;
        }
        Role role2=roleMapper.selByRoleAndName(role, roleName, unitId);
        String[] permIds=perms.split(",");
        for(int i=0; i<permIds.length; i++){
            int addRolePermissionResult=roleMapper.insRolePermission(role2.getRoleId(), Integer.parseInt(permIds[i]));
            if(addRolePermissionResult!=1){
                responseResult.setCode(0);
                responseResult.setMsg("新建角色失败！");
                return responseResult;
            }
        }
        responseResult.setCode(1);
        responseResult.setMsg("新建角色成功！");
        return responseResult;
    }

    @Override
    public ResponseResult deleteRoleById(long unitId, int roleId) {
        ResponseResult responseResult=new ResponseResult();
        int deleteRoleResult=roleMapper.updDeleteRole(roleId+"");
        if(deleteRoleResult!=1){
            responseResult.setCode(0);
            responseResult.setMsg("角色删除失败！");
            return responseResult;
        }
        int deleteRolePermissionResult=roleMapper.updDeleteRolePerms(roleId+"");
        responseResult.setCode(1);
        responseResult.setMsg("角色删除成功！");
        return responseResult;
    }

    @Override
    public Map<String, Object> editRole(int roleId) {
        Map<String, Object> map=new HashMap<>();
        Role role1=roleMapper.selByRoleId(roleId);
        map.put("role", role1);
        List<Integer> permissionIds=roleMapper.selPermissionIdsByRoleId(roleId);
        map.put("permissionIds", permissionIds);
        List<Permission> permissionList=permissionMapper.selPermissions();
        map.put("permissions", permissionList);
        return map;
    }

    @Override
    public ResponseResult executeEditRole(Role role, String perms) {
        int updateRoleResult=roleMapper.updRoleAndName(role);
        int deleteRolePermsResult=roleMapper.delRolePerms(role.getRoleId()+"");
        ResponseResult responseResult=new ResponseResult();
        String[] permIds=perms.split(",");
        for(int i=0; i<permIds.length; i++){
            int addRolePermissionResult=roleMapper.insRolePermission(role.getRoleId(), Integer.parseInt(permIds[i]));
            if(addRolePermissionResult!=1){
                responseResult.setCode(0);
                responseResult.setMsg("编辑角色失败！");
                return responseResult;
            }
        }
        responseResult.setCode(1);
        responseResult.setMsg("编辑角色成功！");
        return responseResult;
    }

    @Override
    public List<Role> getRolesByExaminerId(long examinerId) {
        List<Integer> roleIdsList=roleMapper.selRoleIdsByExaminerId(examinerId);
        List<Role> roleList=new ArrayList<>();
        for(int i=0; i<roleIdsList.size(); i++){
            roleList.add(roleMapper.selByRoleId(roleIdsList.get(i)));
        }
        return roleList;
    }

    @Override
    public Map<String, Object> getRolesAndExaminerRolesByExaminerId(long examinerId) {
        Map<String, Object> map=new HashMap<>();
        map.put("roles", roleMapper.selRolesByUnitId(Long.valueOf((examinerId+"").substring(0, 6))));
        map.put("examinerRoleIds", roleMapper.selRoleIdsByExaminerId(examinerId));
        map.put("examiner", examinerMapper.selExaminerById(examinerId));
        return map;
    }

    @Override
    public ResponseResult executeApportionRole(Long examinerId, String roleIds) {
        int deleteRolesByExaminerIdResult=roleMapper.delRoleByExaminerId(examinerId);
        ResponseResult responseResult=new ResponseResult();
        String[] roles=roleIds.split(",");
        for(int i=0; i<roles.length; i++){
            int addExaminerRoleResult=roleMapper.insExaminerRole(examinerId, Integer.valueOf(roles[i]));
            if(addExaminerRoleResult!=1){
                responseResult.setCode(0);
                responseResult.setMsg("分配角色失败！");
                return responseResult;
            }
        }
        responseResult.setCode(1);
        responseResult.setMsg("分配角色成功！");
        return responseResult;
    }

    @Override
    public Map<String, Object> getExaminerInfo(Examiner examiner) {
        Map<String, Object> map=new HashMap<>();
        List<Role> roleList=roleMapper.selExaminerRoles(examiner.getExaminerId());
        String roleNames="";
        for(Role role:roleList){
            roleNames=roleNames+role.getRoleName()+"，";
        }
        map.put("roleNames", roleNames);
        map.put("examiner", examiner);
        map.put("nation", nationMapper.selByNationId(examiner.getNationId()).getNationName());
        map.put("professional", professionalTitleMapper.selByProfessionalTitleId(examiner.getProfessionalTitleId()));


        return map;
    }

    @Override
    public Role getRoleByName(String role, String roleName, long unitId) {
        return roleMapper.selByRoleAndName(role, roleName, unitId);
    }

}
